//
//  StudioDetailViewController.m
//  Arts1
//
//  Created by lanou on 15/8/29.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "StudioDetailViewController.h"
#import "StudioDetailHeadView.h"
#import "StudioHeaderView.h"
#import "PrefixHeader.pch"
#import "AllHeader.h"

#import "StudioStateCollectionViewController.h"
#import "StudioWorkCollectionViewController.h"
#import "StudioCourseCollectionViewController.h"
#import "StudioContactViewController.h"
// #import "StudioContactViewController.h"

#import "STUDStateViewController.h"
#import "STUDWorkViewController.h"
#import "STUDCourseViewController.h"


#import "SSDetailModel.h"

#import "ShareViewController.h"

// 画室详情页面接口
#define stuDURL @"http://arts1.dlab.com.cn/app/spaceinfo"

// 获取关注状态接口
#define gfsURL @"http://arts1.dlab.com.cn/app/getfollowstatus"

// 添加或取消关注接口
#define sfURL @"http://arts1.dlab.com.cn/app/setfollow"

@interface StudioDetailViewController ()

// 最新动态
@property (nonatomic, strong) StudioStateCollectionViewController *stateVC;

// 课程安排
@property (nonatomic, strong) StudioCourseCollectionViewController *courceVC;

// 优秀作品
@property (nonatomic, strong) StudioWorkCollectionViewController *workVC;

// 联系我们
// @property (nonatomic, strong) StudioContactViewController *contactVC;
@property (nonatomic, strong) StudioContactViewController *contactVC;

// 画室名称和图像视图
@property (nonatomic, strong) StudioHeaderView *headView;

// 自定义navigationbar
@property (nonatomic, strong) StudioDetailHeadView *navigationview;

// 纪录上一次现实的视图的 UIViewController
@property (nonatomic, strong) UIViewController *lastVC;

// 画室详情model
@property (nonatomic, strong)SSDetailModel *studio;

// 分享选项卡页面
@property (nonatomic, strong) ShareViewController *shareVC;

@end

@implementation StudioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
  
    [self createShareView];
    [self createStateView];
    [self requestData];
    [self createHeadView];
    _lastVC = _stateVC;
    

}


//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *pardic = @{@"id":_space_ID};
    
    [manage GET:stuDURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // 请求成功的时候调用这个block
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            // 请求返回的状态，当status = 1 时有数据
            NSInteger status = [dic[@"status"] integerValue];
            if (status == 1) {
                NSDictionary *dict = dic[@"data"];
                // 将数据装换成model类
                _studio = [[SSDetailModel alloc] init];
                [_studio setValuesForKeysWithDictionary:dict];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                _headView.studio = _studio;
             //   _navigationview.studio = _studio;
               
            });
            NSLog(@"画室详情页面 ===数据请求--%@", dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
    
}

- (void)createHeadView{
    
    // 院校名称和图像视图
    _headView = [[StudioHeaderView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH , 114)];
    [self.view addSubview:_headView];
    [_headView.attentionBTN addTarget:self action:@selector(attentionStidio:) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 自定义UINavigationBar
    _navigationview = [[StudioDetailHeadView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    [self.view addSubview:_navigationview];
    // 返回按钮点击事件
    [_navigationview.backButton addTarget:self action:@selector(backBefore) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 分享按钮点击事件
    [_navigationview.shareBTN addTarget:self action:@selector(shareMessage:) forControlEvents:(UIControlEventTouchUpInside)];
    

    
    // 动态、简章、成绩、考题的点击事件的回调block
    __weak StudioDetailViewController *wStuioDVC = self;
    _headView.changeviewblock = ^(NSInteger index){
        // 将上一个视图移除
         [wStuioDVC removeViewOfController];
        // 展示被点击的按钮对应的视图
        switch (index) {
            case 0:{
                [wStuioDVC createStateView];
                wStuioDVC.lastVC = wStuioDVC.stateVC;
            }
                break;
            case 1:{
                [wStuioDVC createCourceview];
                wStuioDVC.lastVC = wStuioDVC.courceVC;
                
            }
                break;
            case 2:{
                [wStuioDVC createWorkview];
                wStuioDVC.lastVC = wStuioDVC.workVC;
            }
                break;
            case 3:{
                [wStuioDVC createContactView];
                wStuioDVC.lastVC = wStuioDVC.contactVC;
            }
                break;
            default:
                break;
        }
        
    };
}
/**
 *  移除视图
 */
- (void)removeViewOfController{
    if ([_lastVC isKindOfClass:[UICollectionViewController class]]) {
        UICollectionViewController *collectionVC = (UICollectionViewController *)_lastVC;
        [collectionVC.collectionView removeFromSuperview];
    } else {
        [_lastVC.view removeFromSuperview];
    }
}

/**
 *  将头部视图和分享视图放最上层
 */
- (void)exchangeHeadAndShareToFront{
    
    [_headView bringSubviewToFront:self.view];

}

// 返回上一级界面
- (void)backBefore{
    [self.navigationController popViewControllerAnimated:YES];
}

// 创建分享选项卡
- (void)createShareView{
    _shareVC = [[ShareViewController alloc] init];
    [self.view addSubview:_shareVC.view];
    _shareVC.view.frame = CGRectMake(0,SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT);
}

// 分享
- (void)shareMessage:(UIButton *)btn{
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        _shareVC.view.frame = CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        
    }];
}

// 创建最新动态的列表页面
- (void)createStateView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10,10, 10);
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.headerReferenceSize = CGSizeMake(0, 0);
    _stateVC = [[StudioStateCollectionViewController alloc] initWithCollectionViewLayout:layout];
      _stateVC.collectionView.frame = CGRectMake(0, 178, SCREEN_WIDTH, SCREEN_HEIGHT - 213);
    [self.view addSubview:_stateVC.collectionView];
    _stateVC.spaceId = _space_ID;
   
    
    __weak StudioDetailViewController *wStuioDVC = self;
    _stateVC.studioStateBlock = ^(NSString *str){
        STUDStateViewController *stateDVC = [[STUDStateViewController alloc] init];
        stateDVC.ID = str;
        [wStuioDVC.navigationController pushViewController:stateDVC animated:YES];
    };
   
}

// 创建课程安排的列表页面
- (void)createCourceview{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10,10, 10);
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _courceVC = [[StudioCourseCollectionViewController alloc] initWithCollectionViewLayout:layout];
    [self.view addSubview:_courceVC.collectionView];
    _courceVC.spaceId = _space_ID;
    _courceVC.collectionView.frame = CGRectMake(0, 178, SCREEN_WIDTH, SCREEN_HEIGHT - 213);
    __weak StudioDetailViewController *wStuioDVC = self;
    _courceVC.studioCourseBlock = ^(NSString *str){
        STUDCourseViewController *courseDVC = [[STUDCourseViewController alloc] init];
        courseDVC.ID = str;
        [wStuioDVC.navigationController pushViewController:courseDVC animated:YES];
    };
    
}

// 创建优秀作品的列表页面
- (void)createWorkview{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10,10, 10);
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _workVC = [[StudioWorkCollectionViewController alloc] initWithCollectionViewLayout:layout];
    [self.view addSubview:_workVC.collectionView];
    _workVC.spaceId = _space_ID;
    _workVC.collectionView.frame = CGRectMake(0, 178, SCREEN_WIDTH, SCREEN_HEIGHT - 213);
    __weak StudioDetailViewController *wStuioDVC = self;
    _workVC.studioWorkBlock = ^(NSString *str){
        STUDWorkViewController *workDVC = [[STUDWorkViewController alloc] init];
        workDVC.ID = str;
        [wStuioDVC.navigationController pushViewController:workDVC animated:YES];
    };
    
}

// 创建联系我们的列表页面
- (void)createContactView{
    _contactVC = [[StudioContactViewController alloc] init];
 //   _contactVC.spaceId = _space_ID;
    [self.view addSubview:_contactVC.view];
    _contactVC.spaceId = _space_ID;
    _contactVC.view.frame = CGRectMake(0, 178, SCREEN_WIDTH, SCREEN_HEIGHT - 213);
}

// 点击关注
- (void)attentionStidio:(UIButton *)btn{
    [self requestGetFollow];
}

// 获取关注状态
- (void)requestGetFollow
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *userDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    //请求参数
    NSDictionary *pardic = @{@"type":@"space",@"uid":userDic[@"data"][@"uid"],@"followuid":_space_ID};
    
    [manage POST:gfsURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         // 请求成功的时候调用这个block
         NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
         //返回的状态
         int index = [dic[@"status"] intValue];
         //  1是已关注，0是未关注
         switch (index) {
             case 0:
                 [self requestSetFollow:0];
                 break;
             case 1:
                 [self requestSetFollow:1];
                 break;
             default:
                 break;
         }
         NSLog(@" 高校关注状态 %@",dic);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
    
}


// 添加或取消关注
- (void)requestSetFollow:(NSInteger)index
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *userDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    //  添加和取消关注的请求参数不一样
    NSString *action = nil;
    
    switch (index) {
        case 0:
            // 未关注，此时需要添加
            action = @"add";
            break;
        case 1:
            // 已关注，此时应该取消
            action = @"cancel";
            break;
        default:
            break;
    }
    
    //请求参数
    NSDictionary *pardic = @{@"type":@"space",@"uid":userDic[@"data"][@"uid"],@"userId":_space_ID,@"key":userDic[@"data"][@"key"],@"act":action};
    
    [manage POST:sfURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         // 请求成功的时候调用这个block
         NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
         //记录请求返回结果
         NSString *statusStr = nil;
         //返回的状态
         int status = [dic[@"status"] intValue];
         switch (status) {
             case 1:
                 statusStr = @"操作成功";
                 break;
             case 0:
                 statusStr = @"用户名或密码错误";
                 break;
             case -1:
                 statusStr = @"参数不正确";
                 break;
             case -2:
                 statusStr = @"获取失败";
                 break;
             case -3:
                 statusStr = @"不能关注自己";
                 break;
             case -4:
                 statusStr = @"已经关注，不需要再次关注";
                 break;
                 
             default:
                 break;
         }
         
         if (statusStr)
         {
             //信息提示框
             [MBProgressHUD show:statusStr icon:nil view:self.view];
         }
         
      //   NSLog(@" 高校添加或取消关注状态 %@",dic);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
    
}




@end
